package com.medicine.hbp.page;

import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.medicine.hbp.R;

public class ViewPageIndexMe extends LinearLayout {
	/**
	 * ����
	 */
	private Paint paint;
	/**
	 * ��
	 */
	private Path path;
	/**
	 * ��
	 */
	private int mTrianglewidth;
	/**
	 * ��
	 */
	private int mTriangleheight;
	/**
	 * ռ������ı���
	 */
	private float RADIO_TRIANGLE_WIDTH = 3 / 5F;
	/**
	 * ��ʼ��ʾ��ƫ����
	 */
	private int InitTranslationX;
	/**
	 * ���ֻ��ƶ�
	 */
	private int TranslationX;
	/**
	 * ���������
	 */
	private int Num;

	public ViewPageIndexMe(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(Color.parseColor("#00ffffff"));
		paint.setStyle(Style.FILL);
		paint.setPathEffect(new CornerPathEffect(10));

	}

	public ViewPageIndexMe(Context context) {
		super(context, null);
	}

	@Override
	protected void dispatchDraw(Canvas canvas) {
		canvas.save();
		canvas.translate(InitTranslationX + TranslationX, getHeight());
		canvas.drawPath(path, paint);
		canvas.restore();
		super.dispatchDraw(canvas);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		RADIO_TRIANGLE_WIDTH = ((float) (Num - 1) / (float) Num);
		mTrianglewidth = (int) (w / Num * RADIO_TRIANGLE_WIDTH);
		InitTranslationX = (w / Num - w / Num / Num * (Num - 1)) / 2;

		initTriangle();
	}

	/**
	 * ��ʼ��Բ��
	 */
	private void initTriangle() {
		// mTriangleheight = mTrianglewidth / 2;
		path = new Path();
		path.moveTo(0, -(getHeight() / 5));
		path.lineTo(mTrianglewidth, -(getHeight() / 5));
		path.lineTo(mTrianglewidth, -(getHeight() / 5 * (5 - 1)));
		path.lineTo(0, -(getHeight() / 5 * (5 - 1)));
		path.close();

	}

	/**
	 * ָʾ��������ָ���й���
	 * 
	 * @param arg0
	 * @param arg1
	 */
	public void scroll(int arg0, float arg1) {
		int tabwidth = getWidth() / Num;
		TranslationX = (int) (tabwidth * (arg0 + arg1));
		
		if(arg0>=(Num-2)&&arg1>0&&getChildCount()>Num){
			if(Num==1){
				this.scrollTo(arg0*tabwidth+(int)(tabwidth*arg1), 0);
			}else{
				this.scrollTo((arg0-(Num-2))*tabwidth+(int)(tabwidth*arg1), 0);	
			}
			
		}
		invalidate();
	}

	public void setNum(int num) {
		Num = num;
	}

	/**
	 * ͷ����ǩ��ȡ
	 */
	private List<String> mTitle;

	/**
	 * ��̬�����view
	 * 
	 * @param title
	 */
	public void setTabItemTitle(List<String> title) {
		if (title != null && title.size() > 0) {
			this.removeAllViews();
			mTitle = title;
			for (int i = 0; i < mTitle.size(); i++) {
				TextView textView = new TextView(getContext());
				LayoutParams lp = new LayoutParams(
						LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
				lp.width = getWight() / Num;
				textView.setLayoutParams(lp);
				textView.setTextSize(15);
				textView.setText(mTitle.get(i));
				textView.setTextColor(getResources().getColor(
						R.color.title_color));
				textView.setGravity(Gravity.CENTER);
				addView(textView);
			}
		}
		setItemColoc(0);
		ItemViewOnClick();
	}

	/**
	 * ��ȡ��Ļ���
	 * 
	 * @return
	 */
	private int getWight() {
		WindowManager manager = (WindowManager) getContext().getSystemService(
				Context.WINDOW_SERVICE);
		return manager.getDefaultDisplay().getWidth();

	}

	private ViewPager pager;

	/**
	 * ��������
	 */
	public void setViewPageItem(ViewPager pager) {
		this.pager = pager;
		pager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) {
				setItemColoc(arg0);
			}
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				scroll(arg0, arg1);
			}
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		pager.setCurrentItem(0);
	}

	/**
	 * ��������ѡ�е���ɫ
	 * 
	 * @param pos
	 */
	private void setItemColoc(int pos) {
		int item_num = getChildCount();
		for (int i = 0; i < item_num; i++) {
			TextView view = (TextView) getChildAt(i);
			view.setTextColor(getResources().getColor(R.color.title_color_03));
		}
		for (int i = 0; i < item_num; i++) {
			TextView view = (TextView) getChildAt(i);
			if (pos == i) {
				view.setTextColor(getResources().getColor(R.color.black));
			}
		}
	}

	/**
	 * �O��ÿ����view���c���¼�
	 */
	public void ItemViewOnClick() {
		int item_num = getChildCount();
		for (int i = 0; i < item_num; i++) {
			View view = getChildAt(i);
			final int j = i;
			if (view instanceof TextView) {
				view.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						pager.setCurrentItem(j);
					}
				});
			}
		}

	}
}
